/*
 * AddressPagerAdapter.java
 * business
 *
 * Created by ChenTao on 2017/3/13.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.address;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * AddressPagerAdapter
 * <p>
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/3/13 0013 16:13
 */
public class AddressPagerAdapter extends PagerAdapter {

    private List<AddressPager> mPagers;

    public AddressPagerAdapter(List<AddressPager> pagers) {
        mPagers = pagers;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View rootView = mPagers.get(position).getRootView();

        if (rootView.getParent() != null) {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }

        container.addView(rootView);
        return rootView;
    }

    @Override
    public int getCount() {
        return mPagers.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mPagers.get(position).getRootView());
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mPagers.get(position).getTitle();
    }

    public List<AddressPager> getPagers() {
        return mPagers;
    }
}
